package com.security.antitheft.module;


import java.io.File;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CallReceiver extends BroadcastReceiver {

	private final int _OUTGOING_CALL = 2;
	private final int _INCOMING_CALL = 1;
	@Override
	public void onReceive(Context context, Intent intent) {

		if (!ShareSetting.isEnableCallLog(context)) return;

		try {
			if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 

				String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

				MyPhoneStateListener phoneListener = new MyPhoneStateListener(context, phonenumber, _OUTGOING_CALL);
				TelephonyManager telephony = (TelephonyManager) context
						.getSystemService(Context.TELEPHONY_SERVICE);
				telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

			} else {
				Bundle bundle = intent.getExtras();
				String state = bundle.getString(TelephonyManager.EXTRA_STATE);
				if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
					String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

					MyPhoneStateListener phoneListener = new MyPhoneStateListener(context, phonenumber, _INCOMING_CALL);
					TelephonyManager telephony = (TelephonyManager) context
							.getSystemService(Context.TELEPHONY_SERVICE);
					telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

				}

			}

		} catch (Exception e) {
		}


	}

	class MyPhoneStateListener extends PhoneStateListener {

		private boolean recording=false; 
		private Context context;
		private MediaRecorder recorder;
		private boolean callState;
		private String phoneNumber, path;
		private int direction;
		private long startTimes, endTimes;

		public MyPhoneStateListener(Context context, String phoneNum, int direction) {
			this.context = context;
			this.phoneNumber = phoneNum;
			this.direction = direction;
		}

		public void onCallStateChanged(int state, String incomingNumber) {
				switch (state) {
				case TelephonyManager.CALL_STATE_IDLE:
					if (direction == _OUTGOING_CALL) {
						if (callState) {
							stopRecording();
							callState = false;
						}
					} else {
						if (callState) {
							stopRecording();
							callState = false;							
						}
						
					}
					break;
				case TelephonyManager.CALL_STATE_OFFHOOK:
					if (direction == _OUTGOING_CALL) {
						if (!callState) {
							callState = true;
							startRecording();
						}
					} else {
						if (!callState) {
							callState = true;
							startRecording();
						}
						
					}
					break;
				case TelephonyManager.CALL_STATE_RINGING:
					break;
				}
			
		}


		public void startRecording(){
			recording = false;
			recorder = new MediaRecorder();
			recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
			recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			path = getFilePath();
			if (path != null) {
				recorder.setOutputFile(path);
				try {
					recorder.prepare();
					startTimes = System.currentTimeMillis();
					recorder.start();   // Recording is now started
					recording = true;
					
				} catch (Exception e) {
				}
				
			}
		}

		public void stopRecording(){
			if (recording) {
				try {
				recorder.stop();
				} catch (Exception e) {
				}
				endTimes = System.currentTimeMillis();
				recorder.reset();   
				recorder.release();
				
			} else {
				endTimes = System.currentTimeMillis();
			}
			QueueHelper queue = new QueueHelper(context, true);
			queue.addCallLog(direction, startTimes, endTimes, phoneNumber, path);
			queue.close();
			queue = null;
		}

		public String getFilePath() {

			try {
				File file;
				file = new File(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir, new Date().getTime()+".3gp");
				return file.getAbsolutePath();
			} catch (NameNotFoundException e) {
			}
			return null;
			
		}
	}
}